home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************
- * *
- * Copyright (C) 1996, Silicon Graphics, Inc. *
- * All Rights Reserved. *
- * *
- * The files in this subtree contain UNPUBLISHED PROPRIETARY SOURCE *
- * CODE of Silicon Graphics, Inc.; the contents of these files may *
- * not be disclosed to third parties, copied or duplicated in any *
- * form, in whole or in part, without the prior written permission *
- * of Silicon Graphics, Inc. *
- * *
- * RESTRICTED RIGHTS LEGEND: *
- * Use, duplication or disclosure by the Government is subject to *
- * restrictions as set forth in subdivision (c)(1)(ii) of the Rights in *
- * Technical Data and Computer Software clause at DFARS 252.227-7013, *
- * and/or in similar or successor clauses in the FAR, DOD or NASA FAR *
- * Supplement. Unpublished - rights reserved under the Copyright Laws *
- * of the United States. *
- * *
- * THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, *
- * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY *
- * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. *
- * *
- * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, *
- * INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY *
- * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, *
- * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY *
- * THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR *
- * PERFORMANCE OF THIS SOFTWARE. *
- **************************************************************************/
-
- /*
- Author : Patrick Bouchaud & Paul Hansen
- galaad@neu.sgi.com hansen@engr.sgi.com
- */
-
- #define MAIN_PROGRAM
-
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <values.h>
- #include <string.h>
- #include <bstring.h>
- #include <sys/mman.h>
-
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include "data.h"
-
- static int W, H, mousex, mousey;
- static float
- ALTMIN, ALTMAX,
- cos_head, sin_head,
- cos_pitch, sin_pitch,
- speed, delta_speed,
- roll, pitch, head;
-
- static void Redraw( void );
- static void Idle( void );
- static void Mouse( int, int, int, int );
- static void Motion( int, int );
- static void Reshape( int, int );
- static void Key( unsigned char, int, int );
-
- static void InitTexture( void );
- static int ReadTerrain( char * );
- void InitializeRowData(int lat_stride, int lon_stride);
- void AfficheTerrain( int, int, int );
-
- float obsLat, obsLon, obsAlt, texture_select;
-
- int XCullDraw(
- float altmin, float altmax,
- int lat_stride, int lon_stride);
-
- /*---------------------------------------------------------------------------*
- * MAIN *
- *---------------------------------------------------------------------------*/
- void
- main( int argc, char *argv[] )
- {
- register int i;
- float val;
-
- glutInit( &argc, argv );
-
- /*
- Local Initializations
- ---------------------
- */
- ReadTerrain( NULL );
-
- obsLat = ((float) (NUM_LATITUDE/2))*D_LATITUDE;
- obsLon = ((float) (NUM_LONGITUDE/2))*D_LONGITUDE;
- obsAlt = 7000.0;
- roll = pitch = head = 0;
- speed = 0.;
-
- head = 0.0;
- pitch = 60.0;
-
- delta_speed = 0.;
- near_cull_dist = 10.;
- far_cull_dist = 16000.;
-
- /*
- Graphics Initializations
- ------------------------
- */
- glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow( "terrain following" );
-
- glutDisplayFunc( Redraw );
- glutReshapeFunc( Reshape );
- glutMouseFunc( Mouse );
- glutIdleFunc(Idle);
- glutPassiveMotionFunc( Motion );
- glutMotionFunc( Motion );
- glutKeyboardFunc(Key);
-
- glEnable( GL_DEPTH_TEST );
- glEnable( GL_CULL_FACE );
- glCullFace( GL_BACK );
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective( 60., 1., near_cull_dist, far_cull_dist );
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- #ifdef TEXTURED
- InitTexture();
- glShadeModel(GL_SMOOTH);
- #else
- glShadeModel(GL_FLAT);
- #endif
-
- InitializeRowData(LAT_STRIDE, LON_STRIDE);
- glClearColor( .5, .5, 1., 1. );
- glClearDepth( 1. );
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glutSwapBuffers();
-
- glutMainLoop();
- }
-
- /*---------------------------------------------------------------------------*
- * Redraw *
- *---------------------------------------------------------------------------*/
- static void
- Redraw( void )
- {
- float aux, alt;
-
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
-
- head += (float)(2*mousex - W) / W;
- pitch += (float)(H - 2*mousey) / H;
-
- #ifdef DEV
- fprintf( stderr, "\n\nobsLat = %f;\n"
- "obsLon = %f;\nobsAlt = %f;\nhead = %f;\npitch = %f;\n\n",
- obsLat, obsLon, obsAlt, head, pitch );
- fprintf( stderr, "\nobsAlt = %f\n", obsAlt );
- #endif
-
- while (pitch < 0.) pitch += 360.;
- while (pitch > 360.) pitch -= 360.;
- while (head < 0.) head += 360.;
- while (head > 360.) head -= 360.;
-
- aux = DEG2RAD((float) head);
- cos_head = fcos( aux );
- sin_head = fsin( aux );
- aux = DEG2RAD((float) pitch);
- cos_pitch = fcos( aux );
- sin_pitch = fsin( aux );
-
- speed += delta_speed;
- obsLat -= speed*cos_head*cos_pitch;
- obsLon += speed*sin_head*cos_pitch;
- obsAlt -= speed*sin_pitch;
-
- alt = Terrain[(int)(obsLat/D_LATITUDE)][(int)(obsLon/D_LONGITUDE)];
- alt += RADADA;
- obsAlt = SUP( alt, obsAlt );
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective( 60., 1., near_cull_dist, far_cull_dist );
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef( pitch, 1.0, 0.0, 0.0 );
- glRotatef( head, 0.0, 1.0, 0.0 );
- glTranslatef(-obsLon,-obsAlt,-obsLat);
-
- XCullDraw( ALTMIN, ALTMAX, LAT_STRIDE, LON_STRIDE);
-
- glutSwapBuffers();
- }
-
- /*---------------------------------------------------------------------------*
- * Idle *
- *---------------------------------------------------------------------------*/
- static void
- Idle( void )
- {
- glutPostRedisplay();
- }
-
- /*---------------------------------------------------------------------------*
- * Mouse *
- *---------------------------------------------------------------------------*/
- static void
- Mouse( int button, int state, int x, int y )
- {
- switch (button)
- {
- case GLUT_LEFT_BUTTON:
- delta_speed = (state==GLUT_DOWN) ? 1. : 0.;
- break;
-
- case GLUT_RIGHT_BUTTON:
- delta_speed = (state==GLUT_DOWN) ? -1. : 0.;
- break;
-
- case GLUT_MIDDLE_BUTTON:
- speed = 0.;
- delta_speed = 0.;
- break;
- }
- }
-
- /*---------------------------------------------------------------------------*
- * Motion *
- *---------------------------------------------------------------------------*/
- static void
- Motion( int x, int y )
- {
- mousex = x;
- mousey = y;
- }
-
- /*---------------------------------------------------------------------------*
- * Reshape *
- *---------------------------------------------------------------------------*/
- static void
- Reshape( int w, int h )
- {
- glViewport( 0, 0, w, h );
- W = w;
- H = h;
- mousex = W/2;
- mousey = H/2;
- }
-
- /*---------------------------------------------------------------------------*
- * Key *
- *---------------------------------------------------------------------------*/
- static void
- Key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27: /* Escape */
- exit(0);
- }
- }
-
- /*---------------------------------------------------------------------------*
- * ReadTerrain *
- *---------------------------------------------------------------------------*/
- static int
- ReadTerrain( char *filename )
- {
- register int i;
- register float *altitude;
- FILE *fp;
-
- fp = fopen("data/elevator.bin", "rb");
- fread(&Terrain[0][0], 1, NUM_LATITUDE*NUM_LONGITUDE*4, fp);
-
- ALTMAX = ALTMIN = Terrain[0][0];
-
- for (i=0, altitude = &Terrain[0][0]; i<NUM_LATITUDE*NUM_LONGITUDE; i++, altitude++) {
- ALTMAX = SUP( ALTMAX, *altitude );
- ALTMIN = INF( ALTMIN, *altitude );
- }
- fclose( fp );
- }
-
- /*---------------------------------------------------------------------------*
- * InitTexture -- the image data is read from a file called "cooked.bin".
- * the data is "cooked" in the sense that it is highly processed: tiled
- * and interlaced to support the texture-select extension for Impact
- * Graphics systems with the TRAM option card. The original image
- * (6000x6000) is placed in the lower-left of a 6Kx6K array; smaller
- * mipmaps are generated and, for each mipmap level, tiles, including
- * border data, are extracted and interlaced in groups of 4 "subtiles".
- *
- * Thus four luminance subtiles get grouped as a single 4-channel
- * (GL_RGBA) "quad-selectable". All of the tiles for one mipmap level
- * are concatenated in "cooked.bin", followed by all the tiles for
- * successively smaller levels.
- *---------------------------------------------------------------------------*/
- #ifdef TEXTURED
-
- static void
- InitTexture( void )
- {
- int i, j, k, lev, tile, subtiles, fptr;
- FILE *fp;
- size_t size, tile_width=TILE_WIDTH, tile_height=TILE_HEIGHT;
- GLenum internalformat, externalformat, externaltype;
- GLubyte *Texture;
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
- glEnable( GL_TEXTURE_2D );
-
- /* We use the proxy-texture extension to test whether the
- texture-select extension is supported. If it is, there
- are 144 quad-selectable textures; if not, there are
- 576 luminance textures. */
-
- glTexImage2D(GL_PROXY_TEXTURE_2D_EXT, 0, GL_QUAD_LUMINANCE8_SGIS, 1, 1, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, NULL);
- texture_select = (int)(glGetError() == GL_NO_ERROR);
-
- /* NOTE: in the case where the texture-select extension is not
- supported, we "work around" the fact that the data is
- interlaced by declaring it to be unsigned int's. These
- int's have valid data (for this pass) in the MS byte, which
- are converted to bytes by the system during download. Then
- we offset by one byte and go through the data again (4 times total). */
-
- fp = fopen("./data/cooked.bin", "rb");
- fprintf(stderr, "lecture de %s: ...", "cooked.bin");
-
- if(texture_select) {
- subtiles = 1;
- internalformat = GL_QUAD_LUMINANCE8_SGIS;
- externalformat = GL_RGBA; /* actually, 4 luminance channels */
- externaltype = GL_UNSIGNED_BYTE;
- }
- else {
- subtiles = 4;
- internalformat = GL_LUMINANCE8_EXT;
- externalformat = GL_LUMINANCE;
- externaltype = GL_UNSIGNED_INT; /* system takes top 8 bits */
- fprintf(stderr, "\ntexture-select not supported on this system.");
- fprintf(stderr, "\nextra processing time required ...");
- }
-
- Texture = (GLubyte*)malloc((TILE_WIDTH+2)*(TILE_HEIGHT+2)*4+3);
- for(lev=fptr=0; lev<MAXLEV; ++lev, tile_width>>=1, tile_height>>=1) {
- size = (tile_width+2)*(tile_height+2)*4;
- for(i=0, tile=1; i<NUMTILE_S; ++i) {
- for(j=0; j<NUMTILE_T; ++j) {
- fread(Texture, size, 1, fp);
- for(k=0; k<subtiles; ++k, tile++) {
- glBindTextureEXT(GL_TEXTURE_2D, tile);
- if(lev == 0) {
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
- GL_LINEAR_MIPMAP_LINEAR);
- }
- glTexImage2D(
- GL_TEXTURE_2D, /* target */
- lev, /* level (mip-mapping) */
- internalformat, /* INTERNAL format */
- tile_width+2, /* texture level width */
- tile_height+2, /* texture level height */
- 1, /* border */
- externalformat, /* EXTERNAL format */
- externaltype, /* EXTERNAL type */
- Texture+k /* data (k=byte offset, see NOTE above) */
- );
- }
- }
- }
- }
-
- fclose(fp);
- free(Texture);
- fprintf(stderr, "Done\n");
- glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glMatrixMode( GL_MODELVIEW );
- }
- #endif /* TEXTURED */
-